package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.VolunteerCertificate;
import com.tju.ocean_ecology_website.dao.VolunteerCertificateDao;
import com.tju.ocean_ecology_website.service.VolunteerCertificateService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 志愿者证书表(VolunteerCertificate)表服务实现类
 *
 * @author shaolong
 * @since 2025-05-05 10:00:00
 */
@Service("volunteerCertificateService")
public class VolunteerCertificateServiceImpl implements VolunteerCertificateService {
    @Resource
    private VolunteerCertificateDao volunteerCertificateDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public VolunteerCertificate queryById(Long id) {
        return this.volunteerCertificateDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param volunteerCertificate 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<VolunteerCertificate> queryByPage(VolunteerCertificate volunteerCertificate, PageRequest pageRequest) {
        long total = this.volunteerCertificateDao.count(volunteerCertificate);
        return new PageImpl<>(this.volunteerCertificateDao.queryAllByLimit(volunteerCertificate, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param volunteerCertificate 实例对象
     * @return 实例对象
     */
    @Override
    public VolunteerCertificate insert(VolunteerCertificate volunteerCertificate) {
        // 设置创建时间和更新时间
        Date now = new Date();
        volunteerCertificate.setCreateTime(now);
        volunteerCertificate.setUpdateTime(now);

        // 设置证书状态
        if (volunteerCertificate.getStatus() == null) {
            volunteerCertificate.setStatus(0); // 默认有效
        }

        this.volunteerCertificateDao.insert(volunteerCertificate);
        return volunteerCertificate;
    }

    /**
     * 修改数据
     *
     * @param volunteerCertificate 实例对象
     * @return 实例对象
     */
    @Override
    public VolunteerCertificate update(VolunteerCertificate volunteerCertificate) {
        // 设置更新时间
        volunteerCertificate.setUpdateTime(new Date());

        this.volunteerCertificateDao.update(volunteerCertificate);
        return this.queryById(volunteerCertificate.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.volunteerCertificateDao.deleteById(id) > 0;
    }

    /**
     * 根据志愿者ID查询证书
     *
     * @param volunteerId 志愿者ID
     * @param pageRequest 分页对象
     * @return 证书列表
     */
    @Override
    public Page<VolunteerCertificate> queryByVolunteerId(Long volunteerId, PageRequest pageRequest) {
        long total = this.volunteerCertificateDao.countByVolunteerId(volunteerId);
        List<VolunteerCertificate> certificates = this.volunteerCertificateDao.queryByVolunteerId(volunteerId, pageRequest);
        return new PageImpl<>(certificates, pageRequest, total);
    }

    /**
     * 生成证书PDF
     *
     * @param id 证书ID
     * @return PDF文件字节数组
     */
    @Override
    public byte[] generateCertificatePdf(Long id) {
        // 查询证书信息
        VolunteerCertificate certificate = this.queryById(id);
        if (certificate == null) {
            return new byte[0];
        }

        // TODO: 使用PDF生成库生成证书PDF
        // 这里简化处理，返回一个空的字节数组
        return new byte[0];
    }

    /**
     * 统计符合条件的数据数量
     *
     * @param volunteerCertificate 查询条件
     * @return 数据数量
     */
    @Override
    public long count(VolunteerCertificate volunteerCertificate) {
        return this.volunteerCertificateDao.count(volunteerCertificate);
    }
}
